package gui;

import business.EventManager;
import business.NotificationManager;
import business.UserManager;
import entity.Event;
import entity.Notification;
import entity.NotificationPK;
import entity.User;
import java.io.Serializable;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import utils.MessageCode;

@Named("notificationBean")
@ViewScoped
public class NotificationBean implements Serializable {

    private int eventId;
    private String participantUsername;
    private String timestamp;
    private Notification notification;
    private boolean viewable = false;

    @EJB
    private NotificationManager notificationManager;
    @EJB
    private UserManager userManager;
    @EJB
    private EventManager eventManager;

    public NotificationBean() {
    }

    public void loadNotification() {
        long time = 0;
        try {
            time = Long.parseLong(timestamp);
        } catch (NumberFormatException ex) {
            Logger.getLogger(NotificationBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        NotificationPK key = new NotificationPK(eventId, participantUsername, new Date(time));
        notification = notificationManager.find(key);
        if (notification == null || notification.getParticipant().equals(userManager.getLoggedUser())) {
            viewable = true;
        }
    }

    public int getEventId() {
        return eventId;
    }

    public void setEventId(int eventId) {
        this.eventId = eventId;
    }

    public String getParticipantUsername() {
        return participantUsername;
    }

    public void setParticipantUsername(String participantUsername) {
        this.participantUsername = participantUsername;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public Notification getNotification() {
        if (notification == null) {
            notification = new Notification();
        }
        return notification;
    }

    public void setNotification(Notification notification) {
        this.notification = notification;
    }

    public boolean isViewable() {
        return viewable;
    }

    public String confirm() {
        notificationManager.delete(notification);
        return "home?faces-redirect=true";
    }

    public String cancel() {
        User currentUser = userManager.getLoggedUser();
        Event event = eventManager.find(notification.getEvent().getEventId());
        currentUser.getCalendars().get(0).removeEvent(event);
        event.removeParticipant(currentUser);
        userManager.save(currentUser);
        eventManager.save(event);
        notificationManager.delete(notification);
        return "log?faces-redirect=true&msgCode=" + MessageCode.PARTICIPATION_CANCELLED.toString();
    }
}
